package com.helloUIBinder.client.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;
import com.helloUIBinder.client.ProjectJsonParser;
import com.helloUIBinder.client.events.OnClosePopupEvent;
import com.helloUIBinder.client.receivers.BlogPostReceiver;
import com.helloUIBinder.model.BlogPost;

import java.util.List;

public class AdminPopupWidget extends Composite {

    private static AdminPopupWidgetUiBinder uiBinder = GWT.create(AdminPopupWidgetUiBinder.class);

    interface AdminPopupWidgetUiBinder extends UiBinder<Widget, AdminPopupWidget> {
    }

    @UiField
    Grid testTable;

    @UiField
    Anchor closeLink;

    private List<BlogPost> blogsList;

    private static final Integer blogPostFieldsCount = 6;
    protected SimpleEventBus eventBus;

    public AdminPopupWidget(SimpleEventBus eventBus) {
        initWidget(uiBinder.createAndBindUi(this));
        BlogPostReceiver.getAllBlogPosts(getAllBlogPostsCallback());

        this.eventBus = eventBus;
    }

    private RequestCallback getAllBlogPostsCallback() {
        return new RequestCallback() {
            @Override
            public void onResponseReceived(Request request, Response response) {
                blogsList = ProjectJsonParser.parseJsonArray(BlogPost.class, response.getText());
                testTable.resize(blogsList.size() + 1, blogPostFieldsCount);
//                testTable.addClickHandler(new ClickHandler() {
//                    public void onClick(ClickEvent event) {
//                        HTMLTable.Cell cell = testTable.getCellForEvent(event);
//                        if (cell.getRowIndex() >= 1 && cell.getCellIndex() == 5) {
//                            RootPanel.get().remove(AdminPopupWidget.this);
//                            AdminBlogPostEditWidget blogPostWidget = new AdminBlogPostEditWidget(blogsList.get(cell.getRowIndex() - 1));
//                            changeBodyTo(blogPostWidget);
//                        }
//                    }
//                });
                for (int row = 0; row < (blogsList.size() + 1); ++row) {
                    if (row == 0) {
                        testTable.setText(row, 0, "ID");
                        testTable.setText(row, 1, "Text");
                        testTable.setText(row, 2, "Owner");
                        testTable.setText(row, 3, "Date");
                        testTable.setText(row, 4, "Theme");
                    } else {
                        testTable.setText(row, 0, String.valueOf(blogsList.get(row - 1).getId()));
                        testTable.setText(row, 1, blogsList.get(row - 1).getText());
                        testTable.setText(row, 2, blogsList.get(row - 1).getOwner());
                        testTable.setText(row, 3, String.valueOf(blogsList.get(row - 1).getDate()));
                        testTable.setText(row, 4, blogsList.get(row - 1).getTheme());
                        Button button = new Button();
                        button.setText("Edit");
                        testTable.setWidget(row, 5, button);

                        button.addClickHandler(new ClickHandler() {
                            public void onClick(ClickEvent event) {
                                HTMLTable.Cell cell = testTable.getCellForEvent(event);
                                if (cell.getRowIndex() >= 1 && cell.getCellIndex() == 5) {
                                    RootPanel.get().remove(AdminPopupWidget.this);
//                                    AdminBlogPostEditWidget adminBlogPostEditWidget = new AdminBlogPostEditWidget(blogsList.get(cell.getRowIndex() - 1));
//                                    changeBodyTo(adminBlogPostEditWidget);
                                }
                            }
                        });
                    }
                }
            }

            @Override
            public void onError(Request request, Throwable exception) {
                Window.alert("Error occurred" + exception.getMessage());
            }
        };
    }

    private void changeBodyTo(Widget widget) {
        BodyContainerWidget currentBodyContainerWidget = (BodyContainerWidget) RootPanel.get().getWidget(1);
        currentBodyContainerWidget.getBodyContainer().remove(0);
        currentBodyContainerWidget.getBodyContainer().add(widget);
    }

    @UiHandler("closeLink")
    void onAdminLinkClick(ClickEvent event) {
        eventBus.fireEvent(new OnClosePopupEvent());
    }
}

